﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace DentistOffice
{
    public partial class Rezerwacja : System.Web.UI.Page
    {

       // DateTime date;
        int patientId;
        bool wasClick = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            //Session["isLogged"] = "44";
            if (Session["isLogged"] == null) Response.Redirect("~/Default.aspx");
            else
            {
                patientId = Int32.Parse(Session["isLogged"].ToString());
            }
            if (IsPostBack)
            {
                wasClick = true;
                
              //  patientId = Int32.Parse(Session["isLogged"].ToString());
            }else{
              //  patientId = Int32.Parse(Session["isLogged"].ToString());
            }
            //Label1.Text = e.CommandName.ToString();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
           
            int hour = 0;
            switch (button.ID.ToString())
            {                   
                case "Button_10":
                    {
                        hour = 10;
                        Labe.Text = "Godzina 10";
                        break;
                    }
                case "Button_11":
                    {
                        hour = 11;
                        Labe.Text = "Godzina 11";
                        break;
                    }
                case "Button_12":
                    {
                        hour = 12;
                        Labe.Text = "Godzina 12";
                        break;
                    }
                case "Button_13":
                    {
                        hour = 13;
                        Labe.Text = "Godzina 13";
                        break;
                    }
                case "Button_14":
                    {
                        hour = 14;
                        Labe.Text = "Godzina 14";
                        break;
                    }
                case "Button_15":
                    {
                        hour = 15;
                        Labe.Text = "Godzina 15";
                        break;
                    }
                case "Button_16":
                    {
                        hour = 16;
                        Labe.Text = "Godzina 16";
                        break;
                    }
                case "Button_17":
                    {
                        hour = 17;
                        Labe.Text = "Godzina 17";
                        break;
                    }
            }
            Panel1.Enabled = false;
            DateTime date = Calendar1.SelectedDate;
            DateTime dt = new DateTime(date.Year,date.Month,date.Day,hour,0,0);
            Labe.Text = dt.ToString();

            bookVisit(dt);
        }

        protected void bookVisit(DateTime date)
        {
            using (DentistEntities dB = new DentistEntities())
            {
                try
                {
                    Book book = new Book();
                    // string ID = Session["hejka"].ToString();
                    //int id = 44;// Int32.Parse(ID);
                    book.idPatient = patientId;
                    book.bookDate = date;
                    //tutaj dołozyc metode o sprawdzaniu czy ktos w trakcie nie rezerwnął sobie miejsca
                    dB.AddToBooks(book);
                    dB.SaveChanges();

                    Labe.Text = patientId.ToString();
                    Panel1.Enabled = true;
                }
                catch (Exception ex)
                {
                    Labe.Text = ex.Message;
                }
            }
        }

        protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
           
            if (wasClick)
            {
                clearButtons();
              
                Panel1.Visible = true;
                makeQuery(Calendar1.SelectedDate);
                //wasClick = false;
            }
        }

        protected void makeQuery(DateTime cl)
        {
           
            using (DentistEntities dB = new DentistEntities())
            {
                try
                {
                    var user = dB.Patients.First(qr => qr.id == patientId);

                    var dentist = dB.Employees.First(qr => qr.id == user.idDentist);
                    Label9.Text = "Doktor: " + dentist.name + " " + dentist.surname;
                    Label10.Text = "Dzień: " + cl.Day + "-" + cl.Month + "-" + cl.Year;
                    var query = dB.Patients.Where(qr => qr.idDentist == user.idDentist );
                    //int c = 0;
                    foreach (Patient pt in query)
                    {
                        foreach (Book bk in pt.Books)
                        {
                            if (bk.bookDate.Year == cl.Year && bk.bookDate.Month == cl.Month && bk.bookDate.Day == cl.Day)
                            {
                                prepareList(bk.bookDate);
                                //c++;
                            }
                        }
                    }
                    //Labe.Text = c.ToString();
                }
                catch (Exception ex)
                {
                    Labe.Text = ex.Message;
                }
            }
        }

        protected void prepareList(DateTime date)
        {
            int hour = date.Hour;

            switch (hour)
            {
                case 10:
                    {
                        Button_10.BackColor = System.Drawing.ColorTranslator.FromHtml("#FF5050");
                        Button_10.Enabled = false;
                        Button_10.Text = "Termin zajęty";
                        break;
                    }
                case 11:
                    {
                        Button_11.BackColor = System.Drawing.ColorTranslator.FromHtml("#FF5050");
                        Button_11.Enabled = false;
                        Button_11.Text = "Termin zajęty";
                        break;
                    }
                case 12:
                    {
                        Button_12.BackColor = System.Drawing.ColorTranslator.FromHtml("#FF5050");
                        Button_12.Enabled = false;
                        Button_12.Text = "Termin zajęty";
                        break;
                    }
                case 13:
                    {
                        Button_13.BackColor = System.Drawing.ColorTranslator.FromHtml("#FF5050");
                        Button_13.Enabled = false;
                        Button_13.Text = "Termin zajęty";
                        break;
                    }
                case 14:
                    {
                        Button_14.BackColor = System.Drawing.ColorTranslator.FromHtml("#FF5050");
                        Button_14.Enabled = false;
                        Button_14.Text = "Termin zajęty";
                        break;
                    }
                case 15:
                    {
                        Button_15.BackColor = System.Drawing.ColorTranslator.FromHtml("#FF5050");
                        Button_15.Enabled = false;
                        Button_15.Text = "Termin zajęty";
                        break;
                    }
                case 16:
                    {
                        Button_16.BackColor = System.Drawing.ColorTranslator.FromHtml("#FF5050");
                        Button_16.Enabled = false;
                        Button_16.Text = "Termin zajęty";
                        break;
                    }
                case 17:
                    {
                        Button_17.BackColor = System.Drawing.ColorTranslator.FromHtml("#FF5050");
                        Button_17.Enabled = false;
                        Button_17.Text = "Termin zajęty";
                        break;
                    }
            }
        }
        protected void clearButtons()
        {
            { Button_10.Text = "Termin wolny"; Button_10.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFF99"); }
            { Button_11.Text = "Termin wolny"; Button_11.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFF99"); }
            { Button_12.Text = "Termin wolny"; Button_12.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFF99"); }
            { Button_13.Text = "Termin wolny"; Button_13.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFF99"); }
            { Button_14.Text = "Termin wolny"; Button_14.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFF99"); }
            { Button_15.Text = "Termin wolny"; Button_15.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFF99"); }
            { Button_16.Text = "Termin wolny"; Button_16.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFF99"); }
            { Button_17.Text = "Termin wolny"; Button_17.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFF99"); }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            //makeQuery();
        }
    }

    

}